package com.chookss.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chookss.R;
import com.chookss.home.adapter.PublishAdapter;
import com.chookss.home.entity.NavigationEntity;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.PermissionSpUtils;
import com.chookss.view.listener.HomeBottomDialogListener;

import java.util.ArrayList;

import io.reactivex.annotations.NonNull;


public class HomeBottomDialog extends Dialog {
    private LinearLayout llMiddle,llBottom,llNewsOriginal,llNewsLink,llChoiceTuwen;
    private RelativeLayout rlAll;
    private TextView tv_cancel;
    private RecyclerView recyclerView;
    private  Context context;
    private  int type=0;
    private PublishAdapter publishAdapter;
    private ArrayList<NavigationEntity> navigationEntityList = new ArrayList<>();
    private HomeBottomDialogListener listener=null;
    public HomeBottomDialog(@NonNull Context context,int type, HomeBottomDialogListener listener) {
        super(context);
        this.context=context;
        this.type=type;
        this.listener = listener;
        initLayoutRes();
    }

    public HomeBottomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initLayoutRes();
    }

    private void initLayoutRes() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_home_bottom, null);
        rlAll=view.findViewById(R.id.rlAll);
        llBottom=view.findViewById(R.id.llBottom);
        llMiddle=view.findViewById(R.id.llMiddle);
        llNewsOriginal=view.findViewById(R.id.llNewsOriginal);
        llNewsLink=view.findViewById(R.id.llNewsLink);
        llChoiceTuwen=view.findViewById(R.id.llChoiceTuwen);
        tv_cancel=view.findViewById(R.id.tv_cancel);
        recyclerView=view.findViewById(R.id.recyclerView);
        if(type==0){
            //导航
            if(PermissionSpUtils.getBoolean(context,"10",false)){
                NavigationEntity navigationEntity=new NavigationEntity();
                navigationEntity.setNavigationName("发视频");
                navigationEntity.setLocalUrl(R.mipmap.icon_shipin2);
                navigationEntityList.add(navigationEntity);
            }
            if(PermissionSpUtils.getBoolean(context,"16",false)){
                NavigationEntity navigationEntity=new NavigationEntity();
                navigationEntity.setNavigationName("发图文");
                navigationEntity.setLocalUrl(R.mipmap.icon_tuwen2);
                navigationEntityList.add(navigationEntity);
            }
            if(PermissionSpUtils.getBoolean(context,"11",false)){
                NavigationEntity navigationEntity=new NavigationEntity();
                navigationEntity.setNavigationName("发新闻");
                navigationEntity.setLocalUrl(R.mipmap.icon_news2);
                navigationEntityList.add(navigationEntity);
            }
            if(PermissionSpUtils.getBoolean(context,"12",false)){
                NavigationEntity navigationEntity=new NavigationEntity();
                navigationEntity.setNavigationName("写日志");
                navigationEntity.setLocalUrl(R.mipmap.icon_rizhi2);
                navigationEntityList.add(navigationEntity);
            }
            if(PermissionSpUtils.getBoolean(context,"18",false)){
                NavigationEntity navigationEntity=new NavigationEntity();
                navigationEntity.setNavigationName("邀请注册");
                navigationEntity.setLocalUrl(R.mipmap.icon_invitation);
                navigationEntityList.add(navigationEntity);
            }
            if(PermissionSpUtils.getBoolean(context,"25",false)){
                NavigationEntity navigationEntity=new NavigationEntity();
                navigationEntity.setNavigationName("发图片");
                navigationEntity.setLocalUrl(R.drawable.icon_ftp_88);
                navigationEntityList.add(navigationEntity);
            }
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
            recyclerView.setLayoutManager(gridLayoutManager);
            publishAdapter = new PublishAdapter(R.layout.item_home_publish, context, navigationEntityList);
            recyclerView.setAdapter(publishAdapter);
            llBottom.setVisibility(View.VISIBLE);
            llMiddle.setVisibility(View.GONE);
            publishAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@androidx.annotation.NonNull BaseQuickAdapter<?, ?> adapter, @androidx.annotation.NonNull View view, int position) {

                    if (listener != null) {
                        if (navigationEntityList.get(position).getNavigationName().equals("发新闻")) {
                            listener.onListItemClick( 2);
                        } else {
                            if (navigationEntityList.get(position).getNavigationName().equals("发视频")) {
                                listener.onListItemClick( 0);
                            } else if (navigationEntityList.get(position).getNavigationName().equals("发图文")){
                                listener.onListItemClick( 1);
                            }else if (navigationEntityList.get(position).getNavigationName().equals("写日志")){
                                listener.onListItemClick( 3);
                            }else if (navigationEntityList.get(position).getNavigationName().equals("邀请注册")){
                                listener.onListItemClick( 4);
                            }else if (navigationEntityList.get(position).getNavigationName().equals("发图片")){
                                listener.onListItemClick( 5);
                            }
                        }
                        dismiss();
                    }
                }
            });
        }else{
            llBottom.setVisibility(View.GONE);
            llMiddle.setVisibility(View.VISIBLE);
        }
        llNewsOriginal.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick( 20);
            }
            dismiss();
        });
        llNewsLink.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick( 21);
            }
            dismiss();
        });
        llChoiceTuwen.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick( 22);
            }
            dismiss();
        });
        tv_cancel.setOnClickListener(v -> {
            dismiss();
        });
        rlAll.setOnClickListener(v -> {
            dismiss();
        });

        setContentView(view);
        initBottom();
    }
    protected void initBottom() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        Window wm = getWindow();
        lp.width = lp.MATCH_PARENT;
        lp.height = lp.MATCH_PARENT;
        lp.dimAmount = 0.6f;
        wm.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        wm.setGravity(Gravity.BOTTOM);
        wm.setWindowAnimations(R.style.DialogFullWidthDimNotEnabled);
        getWindow().setAttributes(lp);
    }
    protected void safeDismiss() {
        if (isShowing()) {
            dismiss();
        }
    }
}
